<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <el-row>
        <!-- 搜索框 -->
        <el-col :span="8" class="inputSc">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            @change="changeSc"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
          <!-- 添加按钮 -->
        </el-col>
        <el-col :span="5"
          ><el-button type="primary" @click="goAddpage"
            >添加按钮</el-button
          ></el-col
        >
      </el-row>
      <!-- 商品列表 -->
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="1000">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="170">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat() }}
          </template>
        </el-table-column>
        <!-- 商品列表编辑与删除 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editShow(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteShowClass(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 编辑商品对话框 -->
    <el-dialog
      title="修改商品"
      :visible.sync="dialogFormVisible"
      @close="closeEditList"
    >
      <el-form :model="editList" :rules="editListRules" ref="editListRef">
        <el-form-item label="商品名称">
          <el-input v-model="editList.goods_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="goods_price">
          <el-input v-model="editList.goods_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="goods_number">
          <el-input v-model="editList.goods_number" type="number"></el-input>
        </el-form-item>
        <el-form-item label="重量" prop="goods_weight">
          <el-input v-model="editList.goods_weight" type="number"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editIpu">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 分页配置对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品数据条数数据
      total: 0,
      // 商品列表数据
      goodsList: [],
      // 编辑商品对话框显示隐藏
      dialogFormVisible: false,
      // 编辑商品 当前商品数据
      editList: [],
      // 修改商品表单验证规则
      editListRules: {
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ]
      }
    }
  },
  //   初始化函数
  created() {
    this.getGoodsList()
  },
  methods: {
    //   获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表数据失败')
      }
      this.total = res.data.total
      this.goodsList = res.data.goods
      this.$message.success('获取商品列表数据成功')
    },
    // 配置当前显示数据条数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 配置当前显示数据页数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 监听搜索框change事件搜索符合条件数据
    changeSc() {
      this.getGoodsList()
    },
    // 发起删除商品信息请求
    async deleteShowClass(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除该商品失败')
      }
      this.getGoodsList()
      this.$message.success('删除商品成功')
    },
    // 编辑商品请求获取当前商品信息
    async editShow(id) {
      const { data: res } = await this.$http.get('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('获取该商品信息失败')
      }
      this.editList = res.data
      this.dialogFormVisible = true
    },
    // 编辑商品对话框关闭事件
    closeEditList() {
      this.$refs.editListRef.resetFields()
      console.log(this.editList)
    },
    // 发起修改商品信息请求
    async editIpu() {
      const { data: res } = await this.$http.put(
        'goods/' + this.editList.goods_id,
        this.editList
      )
      if (res.meta.status !== 200) {
        return this.$message.erroer('修改商品信息失败')
      }
      this.dialogFormVisible = false
      this.getGoodsList()
    },
    // 跳转到add页面
    goAddpage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped>
.box-card {
  margin-top: 20px;
}
.inputSc {
  margin-right: 20px;
}
</style>
